/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package otros;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.font.TextAttribute;
import java.awt.print.*;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.AttributedString;
import java.text.DecimalFormat;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JTable;
import tablasSql.ConsultaBD;

/**
 *
 * @author FERRETERIA
 */


class ObjetoDeImpCompra extends Component implements Printable{
    
    private DatosToImprimir d;
    private int posY;
    private String fuente;
    
    public ObjetoDeImpCompra (DatosToImprimir d) {
        super();
        this.d = d;
        this.fuente = Configuracion.getInstance().getProperty(Configuracion.FUENTE);
    } 
    
    
    private int getIniPosX() { 
       return 13;
    }
    
    private Image getImagen (String url) {
        URL imagenURL = null;
        try {
            imagenURL = new URL(url);
        } catch (MalformedURLException e) {
           e.printStackTrace();
        }
        return Toolkit.getDefaultToolkit().getImage(imagenURL);
    }
    
    public void Encabezado(Graphics g) {
         
        
        int x = getIniPosX();
        int y = 13;
        posY = 8;
        
        g.setFont(new Font (fuente, Font.PLAIN, 9));
        String tipo = "";
            switch(d.getTipoTicket()) {
                case 0: tipo = "Factura:";
                        break;
                case 1: tipo = "Albaran:";
                    break;
                case 2: tipo = "Abono:";
                    break;
            }
        g.drawString("Fecha: " + d.getFecha(), x, posY);         
        g.drawString(tipo+d.getNumTicket(), x, posY+=y);      // x,y
        g.drawLine(x, posY+=5, 210, posY);
    }
    
    private String getCosto(String valor) {
        String costo = "";
        for (int i = 0; i < valor.length(); i++){
            switch(valor.charAt(i)) {
                case'1':
                    costo += "M";
                    break;
                case'2':
                    costo += "U";
                    break;
                case'3':
                    costo += "J";
                    break;
                case'4':
                    costo += "E";
                    break;
                case'5':
                    costo += "R";
                    break;    
                case'6':
                    costo += "C";
                    break;
                case'7':
                    costo += "H";
                    break;
                case'8':
                    costo += "I";
                    break;
                case'9':
                    costo += "N";
                    break;
                case'0':
                    costo += "A";
                    break;
                default:
                    costo += ",";
                    break;
            }
        }
        return costo;
    }
    
    
    public void Datos(Graphics g) {
        int x = getIniPosX();
        int y = 13;
        
        g.setFont(new Font (fuente, Font.PLAIN, 9));
        g.drawString("PRODUCTO", x, posY+=y);
        g.drawString("COSTO", 171, posY);
        g.drawLine(x, posY+=2, 210, posY);
        
        JTable tabla = d.getTabla();
        for (int i = 0; i < tabla.getRowCount()-1; i++) {
            if(!tabla.getValueAt(i, 2).toString().equals("0.0")) {
                String cod = tabla.getValueAt(i, 0)!= null? String.valueOf(tabla.getValueAt(i, 0))+"-": "";
                cod += tabla.getValueAt(i, 1).toString();
                g.setFont(new Font (fuente, Font.PLAIN, 8));
                g.drawString(cod.length()>29?cod.substring(0,29): cod, x, posY += y);
                g.setFont(new Font (fuente, Font.PLAIN, 10));
                g.drawString(getCosto(""+Util.redondeo(Double.parseDouble(""+tabla.getValueAt(i, 7)), 2)), 171, posY);
            }
        }        
    }
    
    public void Pie(Graphics g) {
         
    }
               
    public int print(Graphics g, PageFormat f, int pageIndex){
        switch (pageIndex) {
            case 0:
               Encabezado(g);
               Datos(g);
        //       Pie(g);
               return PAGE_EXISTS;
            default:
                return NO_SUCH_PAGE;
        }
    }
}

public class ImpCompra {
    
    static String impresora;
    
    void Imprimir () {
        setImpresora();
    }
    
    static void setImpresora () {
        impresora = Configuracion.getInstance().getProperty(Configuracion.IMP_TICKET);
    }
    
    static public void imprime(DatosToImprimir d){
        ConsultaBD.cursorEspera(null);
        setImpresora();
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        double margin;
        paper.setSize(30*72, 500);
        margin = 0.5;
        
        paper.setImageableArea(margin, margin, paper.getWidth()-margin, paper.getHeight()-margin);
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.PORTRAIT);  // vertical
        job.setJobName("caja");
        
        //Busco impresora
        AttributeSet attributeSet = new HashAttributeSet();
        attributeSet.add(new PrinterName(impresora, null));
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
        
        try{
           job.setPrintService(services[0]);
           job.setPrintable(new ObjetoDeImpCompra(d), pf);  
           job.print();
           
        } catch(PrinterException e){
             System.out.println(e);
        }

        ConsultaBD.cursorNormal(null);
    }
}
